odoo.define('web_form_dynamic_button', function (require) {
    "use strict";

    var core = require('web.core');
    var FormController = require('web.FormController');
    var _t = core._t;


    FormController.include({
        updateButtons: function () {
            this._super.apply(this, arguments);
            if (this.mode === 'readonly' && this.$buttons && this.hasButtons) {
                var self = this;
                var attrs = this.renderer.arch.attrs;
                var actions = ['edit', 'create'];
                _.each(actions, function (action) {
                    var expr = attrs[action + '_expr'];
                    var act_res = expr ? self._evalActionExpr(expr) : self.activeActions[action];
                    self.$buttons.find('.o_form_button_' + action).toggleClass('o_hidden', !act_res);
                });
            }
        },
        _getActionMenuItems: function () {
            this._super.apply(this, arguments);
            const props = this._super(...arguments);
            // var otherActionItems = props.items.other
            if(props){
                var otherActionItems = props.items.other


                var self = this;
                var attrs = this.renderer.arch.attrs;
                var actions_bar = ['delete', 'duplicate'];
                _.each(actions_bar, function (action) {
                    var capAct = _.string.capitalize(action);
                    var t_label = _t(capAct);
                    var expr = attrs[action + '_expr'];
                    var act_res = expr ? self._evalActionExpr(expr) : self.activeActions[action];
                    debugger
                    if (!act_res) {
                        for (var i = 0;i < otherActionItems.length; i++){
                                if (otherActionItems[i].description== t_label){
                                otherActionItems.splice(i,1);
                                }
                        }
                    }
                });
                return Object.assign(props, {
                    items: Object.assign(this.toolbarActions, { other: otherActionItems }),
                });
            }
        },
        _evalActionExpr: function (expr) {
            var expression = py.parse(py.tokenize(expr));
            return py.PY_isTrue(py.evaluate(expression, this.renderer.state.evalContext));
        }
    });
});